App.controller("warehouseProductCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.search = {};		//搜索参数
	$scope.class_show = true;		//分类（显示）
	$scope.series_show = true;		//系列（显示）
	$scope.shopName_show = true;	//商户名称（显示）
	
	$scope.operateContainer_show = true;		//操作按钮（显示）
	$scope.baseOperate_show = true;		//基本操作（显示）
	$scope.warehouse_show = true;		//选取到库,全部到库（显示）
	
	$scope.priceUnit_hidden = false;	//计价单位（隐藏）
	$scope.isNew_hidden = false;		//是否为新品（隐藏）
	$scope.shopName_hidden = false;		//商户（隐藏）
	
	
	var alertModel = {};	//alert模态框
	
	
	if(userInfo.user_level==0){	//开发者
		$scope.class_show = false;		
		$scope.series_show = false;		
		
		$scope.operateContainer_show = false;
		
		$scope.priceUnit_hidden = true;	
		$scope.isNew_hidden = true;		
	}else if(userInfo.user_level==3){	//三级用户
		$scope.shopName_show = false;
		
		$scope.warehouse_show = false;
		
		$scope.shopName_hidden = true;
	}else if(userInfo.user_level==2){	//二级用户
		$scope.shopName_show = false;
		
		$scope.warehouse_show = false;
	
		$scope.shopName_hidden = true;
	}else if(userInfo.user_level==1){	//一级用户
		$scope.shopName_show = false;
		
		$scope.baseOperate_show = false;
		
		$scope.shopName_hidden = true;
	}
	
	
	
	//表格对象
	$scope.gridOptions = {
			dataSource : {
				transport : {
					read : {
						url : path+"/server/queryproductpage",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									classify_id: $("#productClass").data("kendoDropDownList").value(),
									series_id: $("#productSeries").data("kendoDropDownList").value(),
									product_title: $scope.search.product_title,
									name: $scope.search.shopName,
									iswarehouse: true
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.data;
					},
					total : function(d) {
						return d.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		        
		        console.log("【Grid 数据绑定成功】");
		    	$('.fancybox').fancybox();
		    },
			columns : [
					{    
						title : "序号",
						field : "", 
						width : "50px",
						template:"<input type='checkbox' class='recordClass' value='#: id #' />"+"<span class='row-number subTitle'></span>"
					},
					{
						field : "imgurl",         
						title : "缩略图",     
						width : "100px",
						template: function(e){ 
							var html = ('<a class="fancybox" href="'+e.imgurl+'" data-fancybox-group="gallery_'+e.id+'" title="缩略图">'
											+'<img src="'+e.imgurl+'" class="tableImage"/>'
										+'</a>');
	
							return html;
						}
					},
					{
						field : "product_title",         
						title : "标题",     
						width : "100px"
					},
					{
						field : "product_unit",         
						title : "计价单位",     
						width : "100px",
						hidden: $scope.priceUnit_hidden
					},
					{
						field : "isnew",         
						title : "是否为新品",     
						width : "100px",
						hidden: $scope.isNew_hidden,
						values: [
						         { text: "否", value: 0 },
						         { text: "是", value: 1 }
				        ],
					},
					{
						field : "name",         
						title : "商户",     
						width : "100px",
						hidden: $scope.shopName_hidden
					},
					{
						field : "min_price",         
						title : "单价",     
						width : "100px",
						template: function(e){
					    	return e.min_price+"元—"+e.max_price+"元";
					    }
					},
					{
						field : "",         
						title : "操作",     
						width : "100px",
						template: function(e){
					    	var html = '';
					    	
					    	if(userInfo.user_level==0){	//开发者
					    		html += '<div class="operation" ng-click="lookProduct('+e.id+')">查看</div>';
					    	}else if(userInfo.user_level==3){	//三级用户
					    		html += '<div class="operation" ng-click="editProduct('+e.id+')">编辑</div>';
					    	}else if(userInfo.user_level==2){	//二级用户
					    		html += '<div class="operation" ng-click="editProduct('+e.id+')">编辑</div>';
					    	}else if(userInfo.user_level==1){	//一级用户
					    		html += '<div class="operation" ng-click="lookProduct('+e.id+')">查看</div>';
					    	}

					    	return html;
					    }
					}
			]
	};
	
	//查询
	$scope.search = function(){
		$scope.grid.dataSource.page(1);
	}
	
	//提示信息
	$scope.alertMessage = function(message){
		alertModel.pannel = jqueryAlert({
			content: message,
			modal: true
		});
	}
	
	//全选，取消
	$scope.chooseAll = function(className,flag){
		publicService.chooseAllByClassName(className,flag);
	}
	
	//筛选导出
	$scope.downloadBySearch = function(){
		
	}
	
	//选中导出
	$scope.downloadByChoose = function(){
		
	}
	
	//新增产品
	$scope.addProduct = function(){
		var params = {
				method:"add",
				backUrl:"warehouseProduct"
		};
		$location.path("/addProduct/"+angular.toJson(params));
	}
	
	//改变产品状态
	$scope.changeProductState = function(type,flag){
		$http({
			   url: path+'/server/modifysaleprice/'+type+'/'+flag,
			   method: 'GET'
		}).success(function(data){
			if(data.code==0){
				$scope.alertMessage("成功！");
				$scope.search();
			}else{
				$scope.alertMessage("失败！");
			}
		}).error(function(data){
			console.warn(data);   
		})
	}
	
	//选取到库
	$scope.putWarehouse = function(isAll){
		var param = "";
		if(isAll){
			param = "-1";
		}else{
			var ids = publicService.getChooseValueByClassName("recordClass");
			if(ids.length==0){
				$scope.alertMessage("请选择产品！");
				return;
			}else{
				param = ids.toString();
			}
		}
		
		$http({
			   url: path+'/server/copy/'+param,
			   method: 'GET'
		}).success(function(data){
			if(data.code==0){
				$scope.alertMessage("成功！");
				$scope.search();
			}else{
				$scope.alertMessage("失败！");
			}
		}).error(function(data){
			console.warn(data);   
		})
	}
	
	//查看
	$scope.lookProduct = function(id){
		var params = {
				method:"look",
				id: id,
				backUrl:"warehouseProduct"
			};
		$location.path("/addProduct/"+angular.toJson(params));
	}
	
	//编辑
	$scope.editProduct = function(id){
		var params = {
				method:"edit",
				id: id,
				backUrl:"warehouseProduct"
			};
		$location.path("/addProduct/"+angular.toJson(params));
	}
	
	
	

	publicService.initProductClass("productClass","productSeries");		//初始化【产品分类】
	publicService.initProductSeries("productSeries");	//初始化【产品系列】
	
	
	
	
	
	
	
});